package com.ld.security.server.blog.modules.da.dis;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import com.ld.security.server.blog.modules.da.constant.DaHtmlConstant;
import com.ld.security.server.blog.modules.da.constant.DaRuleConstant;
import com.ld.security.server.blog.modules.da.entity.DaGroupEntity;
import com.ld.security.server.blog.modules.da.entity.DaHtmlEntity;
import com.ld.security.server.blog.modules.da.entity.DaHtmlTelEntity;
import com.ld.security.server.blog.modules.da.entity.DaRuleEntity;
import com.ld.security.server.blog.modules.da.service.IDaGroupService;
import com.ld.security.server.blog.modules.da.service.IDaHtmlService;
import com.ld.security.server.blog.modules.da.service.IDaHtmlTelService;
import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.mybatis.base.po.IdsPo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 文本解析调度
 */
@Component
public class HtmpDispatch {

    @Autowired
    IDaHtmlService iDaHtmlService;

    @Autowired
    IDaHtmlTelService iDaHtmlTelService;

    @Autowired
    IDaGroupService iDaGroupService;

    /**
     * 文件下载
     * @param htmlEntity
     */
    private void one(DaHtmlEntity htmlEntity){
        if(htmlEntity.getHtmlStatus() == DaHtmlConstant.htmlStatus.await){
            String html = HttpUtil.get(htmlEntity.getUrl());
            htmlEntity.setBaseData(html);
        }

        this.two(htmlEntity);
    }


    private void iniDocument(Element document,String baseUrl,String tag,String attr){
        for (Element element : document.select(tag)) {
            String key=attr;
          String src = element.attr(key);
          if(!src.startsWith("http")){
              if(src.startsWith("/")){

              }else{
                  src="/"+src;
              }
              src = baseUrl+src;
          }
          element.attr(key,src);

        }

    }




    /**
     * 预处理处理
     * @param htmlEntity
     */
    private void two(DaHtmlEntity htmlEntity){
        DaHtmlTelEntity htmlTelEntity =  iDaHtmlTelService.getById(htmlEntity.getHtmlTelId());


        String html = htmlEntity.getBaseData();
        Document document =  Jsoup.parse(html);



        DaGroupEntity groupEntity =  iDaGroupService.getById(htmlEntity.getGroupId());

        this.iniDocument(document,groupEntity.getBaseUrl(),"img","src");
        this.iniDocument(document,groupEntity.getBaseUrl(),"a","href");


        List<DaRuleEntity> dataRuiles =  JsonUtil.clone(htmlTelEntity.getHtmlRules(),List.class,DaRuleEntity.class);
        List<DaRuleEntity> pageRuiles =  JsonUtil.clone(htmlTelEntity.getPageRules(),List.class,DaRuleEntity.class);

        HashMap map = new HashMap<>();

        this.three(document,map,dataRuiles);
        this.three(document,map,pageRuiles);


        html = document.html();
    htmlEntity.setBaseData(html);
        String parData =  JsonUtil.toJson(map);
        htmlEntity.setParData(parData);
        htmlEntity.setHtmlStatus(DaHtmlConstant.htmlStatus.dispatch);
        iDaHtmlService.save(htmlEntity);
    }





    /**
     * 数据解析
     * @param document
     * @param args
     * @param rulesList
     * @return
     */
    private Object three(Element document, Map args,List<DaRuleEntity> rulesList){



        for (DaRuleEntity rule : rulesList) {

            DaRuleConstant.fieldType fieldType =  rule.getFieldType();
            if(fieldType == null){
                continue;
            }
            List list = null;
            Elements elements =  document.select(rule.getCssSelect());


                    switch (fieldType){
                        case text:{
                            list = elements.stream().map(v -> {
                                String text = v.text();
                                return text;
                            }).collect(Collectors.toList());
                            break;
                        }case attr:{
                            list = elements.stream().map(v -> {
                                String text =v.attr(rule.getAttr());
                                return text;
                            }).collect(Collectors.toList());
                            break;
                        }case html:{
                            list = elements.stream().map(v -> {
                                String text =v.html();
                                return text;
                            }).collect(Collectors.toList());
                            break;
                        }case down:{
                            break;
                        }case field_group:{

                            List vals =  new LinkedList<>();
                            List<DaRuleEntity> children =  rule.getChildren();

                            for (Element e : elements) {
                                HashMap map =  new HashMap<>();
                                vals.add(map);
                                this.three(e,map,children);
                            }
                            list=vals;
                            break;
                        }
                    }

                    Object val=null;
                    if(list.size() == 1){
                     val=list.get(0);
                    }else{
                        val = list;
                    }



            args.put(rule.getFieldName(),val);
        }
        return args;
    }



    /**
     * 任务调度开始
     * @param po
     * @return
     */
    public int startDispatch(IdsPo po){
        List<DaHtmlEntity> list =  iDaHtmlService.all(po);

        CompletableFuture[] cs =  new CompletableFuture[list.size()];

        for(int i = 0;i<list.size();i++){
            DaHtmlEntity daHtmlEntity = list.get(i);
            //异步并发
            cs[i] =  CompletableFuture.runAsync(() -> {

                this.one(daHtmlEntity);
            });
        }

        //等待
        CompletableFuture.allOf(cs).join();


        return list.size();
    }

}
